-- HitAction
-- create by weism
-- 命中动作

HitAction = class("HitAction", function()
    return ActionBase.create();
end);

function HitAction.create(source, target, skillId)
    local self = HitAction.new();
    self.source = source;
    self.target = target;
    self.skillId = skillId;
    return self;
end

-- 构造函数
function HitAction:ctor()
end

-- 开始播放
function HitAction:play()
    if not UIDungeonMgr:getCurLevel() then
        return;
    end

    -- 播放命中特效
    local effectId = 0;
    local pos = cc.p(0, 0);
    local offset = cc.p(0, 0);
    if self.skillId == PHYSIC_ATTACK then
        effectId = 999;
    else
        effectId = SkillM.query(self.skillId, "hit_effect");
        if effectId == nil or effectId == 0 then
            effectId = 999;
        end
    end

    if self.target == ME.user then
        -- 玩家
        if self.skillId == PHYSIC_ATTACK then
            effectId = 998;
        end
        pos = UIDungeonMgr:getCurLevel():getHeroPos();
        pos = cc.pAdd(pos, cc.p(0, -30));

        -- 播放音效
        local pet = ME.user:getActivePet();
        local modelId = pet:getModelId();
        local audioId = PetM.queryModel(modelId, "attack_audio");
        AudioM.playFx(audioId);
    elseif self.target.type == OBJECT_TYPE_MONSTER then
        -- 怪物
        pos = UIDungeonMgr:getCurLevel():getMonsterPos(self.target:getOwner());

        if self.target:isAudience() then
            -- 观众击中光效可能需要偏移
            local info = NormalAudienceM.queryAudience(self.target.classId, "offset");
            if type(info) == "table" then
                offset = cc.p(info[1], info[2]);
            end
        end

        -- 被附加状态就没有受创音效
        if SkillM.query(self.skillId, "effect_style") ~= ACTIVE_SKILL_DEBUFF then
            -- 播放音效
            local audioId = MonsterM.query(self.target.classId, "damage_audio");

            -- 变形术，青蛙音效
            if #PropM.fetchProps(self.target, "transfiguration") > 0 then
                if self.target:isBoss() then
                    audioId = "frogboss_damage";
                else
                    audioId = "frog_damage";
                end
            end

            -- 变骷髅，骷髅音效
            if #PropM.fetchProps(self.target, "skeleton") > 0 then
                audioId = "skull_damage1";
            end

            AudioM.playFx(audioId);
        end

        -- 如果是召唤兽攻击的，修改光效
        if self.source and self.source.type == OBJECT_TYPE_SUMMON then
            local effect = SummonM.query(self.source.classId, "attack_effect");
            if type(effect) == "number" then
                effectId = effect;
                if self.skillId ~= PHYSIC_ATTACK and tonumber(SkillM.query(self.skillId, "hit_effect")) ~= 0 then
                    effectId = SkillM.query(self.skillId, "hit_effect");
                end
            end
        end

        -- 有时装时，普攻光效替换
        if self.skillId == PHYSIC_ATTACK then
            local pet = ME.user:getActivePet();
            local modelId = pet:getModelId();
            local attackEffectId = PetM.queryModel(modelId, "attack_effect_id");
            if type(attackEffectId) == "number" and attackEffectId > 0 then
                effectId = attackEffectId;
            end
        end
    elseif self.target.type == OBJECT_TYPE_BINE then
        -- 魔藤
        pos = UIDungeonMgr:getCurLevel():getBinePos(self.target:getOwner());
    elseif self.target.type == OBJECT_TYPE_SUMMON then
        -- 召唤兽
        pos = UIDungeonMgr:getCurLevel():getMonsterPos(self.target:getOwner());
    elseif self.target.type == OBJECT_TYPE_GHOST_SHIP then
        -- 幽灵船
        -- 播放音效
        AudioM.playFx("equip_unload");

        pos = UIDungeonMgr:getCurLevel():getMonsterPos(self.target:getPos());
    elseif self.target.type == OBJECT_TYPE_BISHOP then
        -- 主教
        pos = UIDungeonMgr:getCurLevel():getMonsterPos(self.target:getPos());
    end

    local duration, effect = playEffect(UIDungeonMgr:getCurLevel():getEffectParent(), effectId);

    if effect == nil then
        return 0;
    end

    local oldPos = cc.p(effect:getPosition());
    oldPos = cc.pAdd(oldPos, offset);
    effect:setPosition(cc.pAdd(pos, oldPos));

    if self.skillId ~= PHYSIC_ATTACK then
        duration = SkillM.query(self.skillId, "pre_hit_time");

        if duration == nil or duration == 0 or duration == "" then
            duration = 0.03;
        end
    else
        duration = 0.03;
    end
    return duration;
end



return HitAction;